package com.zikao.common.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import com.zikao.article.vo.FileUploadVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zikao.common.entity.SysFileEntity;
import com.zikao.common.service.SysFileService;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 该控制器处理上传文件的相关操作
 *
 * @author likaisheng
 * @email 1805318928@qq.com
 * @date 2021-02-20 11:30:11
 */
@Controller
@RequestMapping("common/sysfile")
public class SysFileController {
    @Autowired
    private SysFileService sysFileService;
    /**
     * 图片上传
     */
    @RequestMapping("/upload")
    @ResponseBody
    public FileUploadVo uploadImage(MultipartFile file) {
        String uuid = UUID.randomUUID().toString();
        boolean result = sysFileService.saveFile(file, uuid);
        FileUploadVo fileUploadVo = new FileUploadVo();
        FileUploadVo.FileData fileData = fileUploadVo.new FileData();

        fileData.setSrc("/common/sysfile/getImage?src="+ sysFileService.getFileAddr("image") + "/" + uuid);
        fileData.setTitle(file.getName());

        fileUploadVo.setCode(result ? 0 : 1);
        fileUploadVo.setData(fileData);

        return fileUploadVo;
    }

    /**
     * 获取服务器上的图片
     * @param src
     * @param request
     * @param response
     */
    @RequestMapping("/getImage")
    @ResponseBody
    public void getImage(String src, HttpServletRequest request, HttpServletResponse response) {
        FileInputStream fis = null;
        response.setContentType("image/gif");
        try {
            OutputStream out = response.getOutputStream();
            File file = new File(src);
            fis = new FileInputStream(file);
            byte[] b = new byte[fis.available()];
            fis.read(b);
            out.write(b);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysFileService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        SysFileEntity sysFile = sysFileService.getById(id);

        return R.ok().put("sysFile", sysFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody SysFileEntity sysFile) {
        sysFileService.save(sysFile);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody SysFileEntity sysFile) {
        sysFileService.updateById(sysFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids) {
        sysFileService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
